﻿namespace DslEditorPowerToy.Controls.Editors
{
    using DslEditorPowerToy.Controls;
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Windows.Forms;

    public class SelectionTrackerEditor : UserControl, IMonitorSelection, ISelectionService
    {
        private IContainer components = null;
        private Label label1;
        private Label label2;
        private ListBox selectableItemsListBox;
        private Label selectedItemsLabel;
        private CheckBox selectionSourceCheckBox;

        public event EventHandler SelectionChanged;

        public event EventHandler SelectionChanging;

        public SelectionTrackerEditor()
        {
            this.InitializeComponent();
            this.InitialiseControls();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        public bool GetComponentSelected(object component)
        {
            return (null != this.selectableItemsListBox.SelectedItem);
        }

        public ICollection GetSelectedComponents()
        {
            return this.selectableItemsListBox.SelectedItems;
        }

        private void InitialiseControls()
        {
            this.selectionSourceCheckBox.Checked = false;
            this.selectedItemsLabel.Tag = "(none)";
            this.selectedItemsLabel.Text = this.selectedItemsLabel.Tag.ToString();
            this.selectableItemsListBox.Items.Add(new SampleItem("SampleItem1", Color.FromKnownColor(KnownColor.Red)));
            this.selectableItemsListBox.Items.Add(new SampleItem("SampleItem2", Color.FromKnownColor(KnownColor.Green)));
            this.selectableItemsListBox.Items.Add(new SampleItem("SampleItem3", Color.FromKnownColor(KnownColor.Blue)));
        }

        private void InitializeComponent()
        {
            this.label1 = new Label();
            this.selectedItemsLabel = new Label();
            this.label2 = new Label();
            this.selectableItemsListBox = new ListBox();
            this.selectionSourceCheckBox = new CheckBox();
            base.SuspendLayout();
            this.label1.AutoSize = true;
            this.label1.Location = new Point(3, 9);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x7c, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Currently Selected Items:";
            this.selectedItemsLabel.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.selectedItemsLabel.BackColor = SystemColors.Control;
            //this.selectedItemsLabel.BorderStyle = BorderStyle.FixedSingle;
            this.selectedItemsLabel.Location = new Point(7, 0x1a);
            this.selectedItemsLabel.Name = "selectedItemsLabel";
            this.selectedItemsLabel.Size = new Size(0x14c, 0x52);
            this.selectedItemsLabel.TabIndex = 2;
            this.label2.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
            this.label2.AutoSize = true;
            this.label2.Location = new Point(3, 0x6d);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x58, 13);
            this.label2.TabIndex = 3;
            this.label2.Text = "Selectable Items:";
            this.selectableItemsListBox.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom;
            this.selectableItemsListBox.IntegralHeight = false;
            this.selectableItemsListBox.Location = new Point(7, 0x7d);
            this.selectableItemsListBox.Name = "selectableItemsListBox";
            this.selectableItemsListBox.SelectionMode = SelectionMode.MultiExtended;
            this.selectableItemsListBox.Size = new Size(0x14c, 0x39);
            this.selectableItemsListBox.TabIndex = 4;
            this.selectableItemsListBox.SelectedIndexChanged += new EventHandler(this.selectableItemsListBox_SelectedIndexChanged);
            this.selectionSourceCheckBox.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.selectionSourceCheckBox.AutoSize = true;
            this.selectionSourceCheckBox.Location = new Point(0xcc, 8);
            this.selectionSourceCheckBox.Name = "selectionSourceCheckBox";
            this.selectionSourceCheckBox.Size = new Size(0x84, 0x11);
            this.selectionSourceCheckBox.TabIndex = 1;
            this.selectionSourceCheckBox.Text = "Only diagram selection";
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            //base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.selectableItemsListBox);
            base.Controls.Add(this.label2);
            base.Controls.Add(this.selectedItemsLabel);
            base.Controls.Add(this.selectionSourceCheckBox);
            base.Controls.Add(this.label1);
            base.Name = "SelectionTrackerEditor";
            base.Size = new Size(0x15d, 0xbd);
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        public virtual void NotifySelectionChanged(ISelectionProvider selectionProvider, SelectionChangeSource source)
        {
            this.selectedItemsLabel.Text = this.selectedItemsLabel.Tag.ToString();
            if ((!this.selectionSourceCheckBox.Checked || (SelectionChangeSource.Diagram == source)) && ((selectionProvider != null) && (selectionProvider.GetSelectedComponents() != null)))
            {
                StringBuilder builder = new StringBuilder();
                ArrayList list = new ArrayList(selectionProvider.GetSelectedComponents());
                foreach (object obj2 in list)
                {
                    if (obj2 != list[0])
                    {
                        builder.Append(", \r\n");
                    }
                    builder.AppendFormat("{0}", obj2);
                }
                string str = builder.ToString();
                if (!string.IsNullOrEmpty(str))
                {
                    this.selectedItemsLabel.Text = str;
                }
            }
        }

        protected void OnSelectionChanged()
        {
            if (this.SelectionChanged != null)
            {
                this.SelectionChanged(this, new EventArgs());
            }
        }

        protected void OnSelectionChanging()
        {
            if (this.SelectionChanging != null)
            {
                this.SelectionChanging(this, new EventArgs());
            }
        }

        private void selectableItemsListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.OnSelectionChanged();
        }

        public void SetSelectedComponents(ICollection components)
        {
            if ((components != null) && (components.Count != 0))
            {
                foreach (object obj2 in components)
                {
                    if ((obj2 is SampleItem) && (this.selectableItemsListBox.Items.IndexOf(obj2 as SampleItem) >= 0))
                    {
                        this.selectableItemsListBox.SelectedItems.Add(obj2 as SampleItem);
                    }
                }
            }
        }

        public void SetSelectedComponents(ICollection components, SelectionTypes selectionType)
        {
            this.SetSelectedComponents(components);
        }

        public object PrimarySelection
        {
            get
            {
                return this.selectableItemsListBox.SelectedItem;
            }
        }

        public int SelectionCount
        {
            get
            {
                return this.selectableItemsListBox.SelectedItems.Count;
            }
        }
    }
}

